/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	/// 
	/// module : battleLogic
	/// </summary>
	public class BattleTeamViewPOD : IPOD 
    {
        public readonly static Func<BattleTeamViewPOD> FACTORY = () => new BattleTeamViewPOD();
		public static Func<BattleTeamViewPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        /// 
        /// </summary>
		public byte teamIndex;
		/// <summary>
        /// 
        /// </summary>
        private List<BattleUnitViewPOD> m_units;
		/// <summary>
        /// 
        /// </summary>
		public BattleUnitPOD substitute;
		
		
        public List<BattleUnitViewPOD> units
        {
            get
            {
                if(m_units == null)
                {
                    m_units = new();
                }
                return m_units;
            }
            private set => m_units = value;
        }
        public List<BattleUnitViewPOD> unsafe_units
        {
        	get => m_units;
        	set => m_units = value;
        }
		
		/*-- constructors --*/
		public BattleTeamViewPOD() {}
		public BattleTeamViewPOD(byte teamIndex, List<BattleUnitViewPOD> units, BattleUnitPOD substitute) {
			this.teamIndex = teamIndex;
			this.m_units = units;
			this.substitute = substitute;
		}
	      public void Serial(SOutputStream _out) {
            _out.Write_ubyte(teamIndex);
            _out.Write_list_pod(m_units);
            _out.Write_pod(substitute);
        }
        
        public void UnSerial(SInputStream _in) {
            if(!_in.HasRemaining()) return;
            teamIndex = _in.Read_ubyte();
            if(!_in.HasRemaining()) return;
            m_units = _in.Read_list_pod<BattleUnitViewPOD>(BattleUnitViewPOD.DynamicFactory);
            if(!_in.HasRemaining()) return;
            substitute = _in.Read_pod<BattleUnitPOD>(BattleUnitPOD.DynamicFactory);
        }
		
		public BattleTeamViewPOD Clone() {
            BattleTeamViewPOD o = DynamicFactory();
            o.teamIndex = teamIndex;
            if (m_units != null) {
                o.m_units = m_units.ConvertAll(o => o?.Clone());
            }
            if (substitute != null) {
                o.substitute = substitute.Clone();
            }
            return o;
        }
        
        public void Clear() {
            teamIndex = default;
            m_units = default;
            substitute = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "BattleTeamViewPOD [";
			str += "teamIndex=";
			str += teamIndex;
			str += ", units=";
			str += m_units.ToString<BattleUnitViewPOD>();
			str += ", substitute=";
			str += substitute;
			str += "]";
			return str;
		}
		
	}
}